package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.PmsBrand;
import com.atguigu.gulimall.product.dao.PmsBrandMapper;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.IPmsBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import java.util.Map;

/**
 * <p>
 * 品牌 服务实现类
 * </p>
 *
 * @author szh
 * @since 2020-12-01
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {
	@Autowired
	private PmsBrandMapper pmsBrandMapper;

	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
		String key = (String) params.get("key");
		if (!StringUtils.isBlank(key)){
			queryWrapper.eq("brand_id",key).or().like("name",key);
		}
		IPage<PmsBrand> page = this.page(
				new Query<PmsBrand>().getPage(params),
				queryWrapper
		);

		return new PageUtils(page);
	}

	@Override
	public PmsBrand selectTest() {
		return pmsBrandMapper.selectTest();
	}

	@Override
	public Boolean updateDetail(PmsBrand brand) {
		// 保证冗余字段的数据一致
		this.updateById(brand);
		if (StringUtils.isNotBlank(brand.getName())){
			// 同步更新其他关联表中的数据
			categoryBrandRelationService.updateBrandName(brand.getBrandId(),brand.getName());
		}
		// todo 同步更新其他表
		return null;
	}
}
